<?php
/**
 * @author writethesky <writethesky@163.com>
 */

$config = require_once "./config.php";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //判断是否有上传文件
    if (is_uploaded_file($_FILES['img']['tmp_name'])) {
        $upfile = $_FILES['img'];

        $name = $upfile['name'];    //文件名
        $type = $upfile['type']; //文件类型
        $size = $upfile['size']; //文件大小
        $tmp_name = $upfile['tmp_name'];  //临时文件
        $error = $upfile['error']; //出错原因
        if ($config['MAX_FILE_SIZE'] < $size) { //判断文件的大小
            echo '上传文件太大';
            exit ();
        }
        if (!in_array($type, $config['UPTYPES'])) {        //判断文件的类型
            echo '上传文件类型不符' . $type;
            exit ();
        }
        if (!file_exists($config['DESTINATION_FOLDER'])) {
            mkdir($config['DESTINATION_FOLDER']);
        }

        $pinfo = pathinfo($name);
        $ftype = $pinfo['extension'];
        $destination = $config['DESTINATION_FOLDER'] . time() . "." . $ftype;
        if (file_exists($destination) && $overwrite != true) {
            echo "同名的文件已经存在了";
            exit ();
        }
        if (!move_uploaded_file($tmp_name, $destination)) {
            echo "移动文件出错";
            exit ();
        }

        // echo $destination;  //" 已经成功上传 文件名: " . 
        
        $tmparr = explode("/", $_SERVER['PHP_SELF']);
        array_pop($tmparr);
        echo ($_SERVER['HTTPS'] != "on" ? "http" : "https") . "://" . $_SERVER['HTTP_HOST'] . implode($tmparr, "/") . "/" . $destination;
     	
        if ($config['WATERMARK'] == 1) {
            require_once "water.php";
        }
    }
}